package com.ufo.algorithm.sorting;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.Stopwatch;

class SortComparison {
    public static double time(Sort alg, Comparable[] a){
        Stopwatch timer = new Stopwatch();
        alg.sort(a);
        return timer.elapsedTime();
    }
    /*T个N长度的数组排序*/
    public static double timeRandomInput(Sort alg, int N, int T){
        double total=0.0;
        Double[] a=new Double[N];
        for(int t=0; t<T; t++){
            for(int i=0; i<N; i++){
                a[i]= StdRandom.uniform();
            }
            total+=time(alg,a);
        }
        return total;
    }

    public static void main(String[] args){
//        Sort alg1 = new PopSort();
        Sort alg1 = new InsertSortV2();
        Sort alg2 = new SelectSort();
        int N = 50;
        int T = 1000;
        double t1 = timeRandomInput(alg1, N, T);
        double t2 = timeRandomInput(alg2, N, T);
        StdOut.printf("For %d random Doubles\n %s is\n",N,alg1);
        StdOut.printf("%.1f times faster than %s\n ",t2/t1,alg2);
    }

}
